<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Cart;
use app\common\model\ProductCate;
use think\facade\Cache;
use think\facade\View;
use think\response\Json;
use app\common\model\Product as ProductModel;

class Product extends Base
{
    public function index()
    {
        $cate_1 = $this->request->param('cate_1');
        $keyword = $this->request->param('keyword');
        $cate = ProductCate::where('id', $cate_1)->value('title');
        View::assign([
            'title' => $keyword ?: $cate,
            'cate_1' => $cate_1,
            'keyword' => $keyword
        ]);

        return View::fetch();
    }

    public function search()
    {
        $history = Cache::get('searchHistory_' . $this->user->id);
        if ($history) {
            $history = json_decode($history, true);
            $news = [];
            for ($i = count($history); $i > 0; $i--) {
                $news[] = $history[$i - 1];
            }
            $history = $news;
        }
        View::assign('history', $history ?: []);

        return View::fetch();
    }

    public function clearSearch()
    {
        Cache::delete('searchHistory_' . $this->user->id);

        return json(['code' => 1, 'msg' => '已清空']);
    }

    public function details($id)
    {
        $info = ProductModel::where(['id' => $id, 'status' => 1, 'is_del' => 0])->find();
        if (empty($info)) return redirect('/');
        $info->images = $info->images ? explode(',', $info->images) : '';
        View::assign('info', $info);
        View::assign('cart_num', Cart::where(['uid' => $this->user->id])->count());

        return View::fetch();
    }

    public function getList(): Json
    {
        $param = $this->request->param();
        $map = [];
        foreach ($param as $key => $value) {
            if (!in_array($key, ['type', 'keyword', 'page', 'limit']) && $value) {
                $map[$key] = $value;
            }
        }
        $like = [];
        if (!empty($param['keyword'])) {
            $like['field'] = 'title';
            $like['keyword'] = $param['keyword'];
            $history = Cache::get('searchHistory_' . $this->user->id);
            if ($history) {
                $history = json_decode($history, true);
                if (!in_array($param['keyword'], $history)) {
                    $history[] = $param['keyword'];
                    Cache::set('searchHistory_' . $this->user->id, json_encode($history));
                }
            } else {
                $history[] = $param['keyword'];
                Cache::set('searchHistory_' . $this->user->id, json_encode($history));
            }
        }
        $sort = [['key' => 'sort', 'value' => 'desc'], ['key' => 'id', 'value' => 'asc']];
        $list = ProductModel::getPageList($map, '*', $param['page'], 20, true, [], [], $sort, $like);

        return json($list);
    }

    public function submit()
    {
        $param = $this->request->post();

        return json(ProductModel::submit($param, $this->user));
    }
}